home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1531 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  65 lines

  1. Path: info.uah.edu!oreo!gbacon
  2. From: gbacon@oreo (Greg Bacon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with simple code
  5. Date: 14 Jan 1996 22:35:07 GMT
  6. Organization: The University of Alabama in Huntsville
  7. Distribution: world
  8. Message-ID: <4dc0er$9ug@info.uah.edu>
  9. References: <4dbak5$oij@ionews.io.org>
  10. NNTP-Posting-Host: oreo.aspire.cs.uah.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. John Gordon MacPherson (jgordon@io.org) wrote:
  14. : Can anyone tell me what's wrong with this piece of code? I lifted it
  15. : straight from a textbook.
  16.  
  17. : Here's the code:
  18.  
  19. : /* Calculating compound interest */
  20. : #include <stdio.h>
  21. : #include <math.h>
  22.  
  23. : main()
  24. : {
  25. :     int year;
  26. :     double amount, principal = 1000, rate = 0.5;
  27.  
  28. :     printf("%4s%21s\n", "Year", "Amount on deposit");
  29.  
  30. :     for (year = 1; year <= 10; year++) {
  31. :         amount = principal * pow(1.0 + rate, year);
  32. :         printf("%4d%21.2f\n", year, amount);
  33. :     }
  34.  
  35. :     return 0;
  36. : }
  37. : ______________________________________________________________
  38.  
  39. : and here's the error:
  40.  
  41. : In function `main':
  42. : undefined reference to `pow'
  43.  
  44. : I don't understand. `pow' is a function, not a variable?
  45. : Can anyone tell me what's wrong?
  46.  
  47. In addition, you need to check your compiler's documentation to see
  48. whether you need to link against any external libraries.  Something
  49. else that you should be aware of is that linkers see functions and
  50. variables as basically the same thing.  Depending on your compiler,
  51. functions themselves are nothing special, just pointers themselves.
  52. Others necessitate that a pointer to function "be 'turned into' a
  53. 'real' function with the * operator" (see question 4.12 of the FAQ).
  54.  
  55. It all depends on how one's viewpoint, but a real philosophical
  56. question (yes, philosophy is very much a part of C programming) is
  57. "what is the true nature of functions?" :)
  58.  
  59. Food for thought,
  60. Greg
  61. --
  62. Greg Bacon <gbacon@cs.uah.edu>
  63. University of Alabama in Huntsville
  64. CS Department Systems Support Team
  65.